package org.mixed.script;

import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.mixed.antlr4.MixScriptLexer;
import org.mixed.antlr4.MixScriptParser;

import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptException;
import java.io.IOException;
import java.io.Reader;

public class MixScriptCompiler implements Compilable {

    @Override
    public CompiledScript compile(String script) {
        CharStream input = CharStreams.fromString(script);
        return compile(input);
    }

    @Override
    public CompiledScript compile(Reader script) throws ScriptException {
        try {
            CharStream input = CharStreams.fromReader(script);
            return compile(input);
        } catch (IOException e) {
            throw new ScriptException(e);
        }
    }

    private CompiledScript compile(CharStream input) {
        MixScriptLexer lexer = new MixScriptLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        MixScriptParser parser = new MixScriptParser(tokens);
        MixScriptParser.ProgramContext tree = parser.program();
        MixScriptVisitor visitor = new MixScriptVisitor().visit(tree);
        return null;
    }
}
